package com.briup.cms.web.controller;

import com.briup.cms.bean.BaseUser;
import com.briup.cms.service.IBaseUserService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(tags="用户管理模块相关接口")
@RequestMapping("/auth/user")
public class BaseUserController {

    @Autowired
    private IBaseUserService service;
    @ApiOperation("根据id查询用户")
    @GetMapping("/select")
    public Message selectByPrimaryKey(@RequestParam(value = "userid",required = false) Integer id){
        BaseUser baseUser=service.selectByPrimaryKey(id);
        return MessageUtil.success(baseUser);
    }
    @ApiOperation("添加")
    @PostMapping("/insert")
    public Message insert(BaseUser baseUser){
        service.insert(baseUser);
        return MessageUtil.success(baseUser);
    }
    @ApiOperation("修改")
    @PutMapping("/update")
    public Message updateByPrimaryKey(BaseUser baseUser){
        service.update(baseUser);
        return MessageUtil.success(baseUser);
    }


    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public Message deleteByPrimaryKey(Integer id){
        service.delete(id);
        return MessageUtil.success(id);
    }
    @ApiOperation("获取用户状态")
    @GetMapping("/info")
    public Message info(@ApiIgnore @RequestHeader("Authorization") String token){
        BaseUser user = service.currentUserInfo(token);
        return MessageUtil.success(user);
    }
    @ApiOperation("分页查询用户信息")
    @GetMapping({"/page/query"})
    public Message findUsersWithPage(int pageNum, int pageSize, String username, Integer role) {
        PageInfo<BaseUser> data = service.pageQuery(pageNum, pageSize, username, role);
        return MessageUtil.success(data);
    }


    @ApiOperation("修改用户头像")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"), @ApiImplicitParam(name = "userIcon", value = "用户头像", required = true, paramType = "query")})
    @PutMapping({"/updateUserIcon"})
     public Message updateUserIcon(int id, String userIcon) {
        service.updateUserIcon(id, userIcon);
        return MessageUtil.success();
    }

}
